home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Mark Pilgrim / Ghost 1.0 / source / Ghost ƒ / MSG Shell ƒ / msg prefs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-30  |  9.2 KB  |  417 lines  |  [TEXT/KAHL]

  1. /**********************************************************************\
  2.  
  3. File:        msg prefs.c
  4.  
  5. Purpose:    This module handles creating/opening/closing/updating
  6.             the preference file, and copying the preference file
  7.             data into application globals (and back).
  8.  
  9.  
  10. Ghost -=- a classic word-building challenge
  11. Copyright (C) 1993 Mark Pilgrim
  12.  
  13. This program is free software; you can redistribute it and/or modify
  14. it under the terms of the GNU General Public License as published by
  15. the Free Software Foundation; either version 2 of the License, or
  16. (at your option) any later version.
  17.  
  18. This program is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21. GNU General Public License for more details.
  22.  
  23. You should have received a copy of the GNU General Public License
  24. along with this program in a file named "GNU General Public License".
  25. If not, write to the Free Software Foundation, 675 Mass Ave,
  26. Cambridge, MA 02139, USA.
  27.  
  28. \**********************************************************************/
  29.  
  30. #include "msg prefs.h"
  31. #include "msg dialogs.h"
  32. #include "msg graphics.h"
  33. #include "msg environment.h"
  34. #include "msg sounds.h"
  35. #include "util.h"
  36. #include "ghost globals.h"
  37.  
  38. long                gFileID;
  39. Str255                gMyName;
  40. Str255                gMyOrg;
  41. Boolean                gCanSavePrefs;
  42.  
  43. static PrefStruct    thePrefs;
  44. static long            gPrefsFilePos;
  45.  
  46. int PreferencesInit(void)
  47. {
  48.     int            prefsFileID;
  49.     int            err;
  50.     
  51.     gCanSavePrefs=FALSE;
  52.     err=GetFileID();
  53.     if (err!=prefs_allsWell)
  54.         return err;
  55.     
  56.     err=OpenPrefsFile(&prefsFileID);
  57.     if (err!=prefs_allsWell)
  58.     {
  59.         if ((err==prefs_diskReadErr) || (err==prefs_diskWriteErr) || (err==prefs_virginErr))
  60.             ClosePrefsFile(prefsFileID);
  61.         return err;
  62.     }
  63.     
  64.     err=CheckVersion(prefsFileID);
  65.     if (err!=prefs_allsWell)
  66.     {
  67.         ClosePrefsFile(prefsFileID);
  68.         return err;
  69.     }
  70.     
  71.     GetFPos(prefsFileID, &gPrefsFilePos);
  72.     gPrefsFilePos-=sizeof(thePrefs);
  73.     do
  74.     {
  75.         gPrefsFilePos+=sizeof(thePrefs);
  76.         err=GetNextPrefs(prefsFileID);
  77.         if (err==prefs_noMorePrefsErr)
  78.             return (Virgin(prefsFileID));
  79.         
  80.         if (err!=prefs_allsWell)
  81.         {
  82.             ClosePrefsFile(prefsFileID);
  83.             return err;
  84.         }
  85.         
  86.         err=CheckFileID();
  87.     }
  88.     while (err==prefs_IDNotMatchErr);
  89.     
  90.     CopyPrefsToGlobals();
  91.     ClosePrefsFile(prefsFileID);
  92.     
  93.     return prefs_allsWell;
  94. }
  95.  
  96. void PrefsError(int err)
  97. {
  98.     Str255            tempStr;
  99.     
  100.     switch (err)
  101.     {
  102.         case prefs_diskReadErr:
  103.         case prefs_diskWriteErr:
  104.         case prefs_cantCreatePrefsErr:
  105.         case prefs_cantOpenPrefsErr:
  106.         case prefs_versionNotSupportedErr:
  107.             DefaultPrefs();
  108.             gCanSavePrefs=FALSE;
  109.             GetIndString(tempStr, 128, -err);
  110.             ParamText(tempStr, "\p", "\p", "\p");
  111.             PositionDialog('ALRT', prefsErrorAlert);
  112.             StopAlert(prefsErrorAlert, 0L);
  113.             break;
  114.         default:
  115.             gCanSavePrefs=TRUE;
  116.             if (err!=prefs_virginErr)
  117.                 ShowSplashScreen();
  118.             break;
  119.     }
  120. }
  121.  
  122. int OpenPrefsFile(int *prefsFileID)
  123. {
  124.     int                thisFile;
  125.     OSErr            isHuman;
  126.     int                vRefNum;
  127.     long            dirID;
  128.     FSSpec            prefsFile;
  129.     FInfo            prefsInfo;
  130.     Boolean            newPrefs;
  131.     unsigned char    *name=PREFS_FILE_NAME;
  132.     
  133.     newPrefs=FALSE;
  134.     isHuman=FindFolder(kOnSystemDisk, 'pref', kCreateFolder, &vRefNum, &dirID);
  135.     
  136.     if (isHuman!=noErr)
  137.         return prefs_cantOpenPrefsErr;
  138.     if (gHasFSSpecs)
  139.     {
  140.         isHuman=FSMakeFSSpec(vRefNum, dirID, name, &prefsFile);
  141.         if (isHuman!=noErr)
  142.         {
  143.             if (isHuman==fnfErr)
  144.             {
  145.                 isHuman=FSpCreate(&prefsFile, CREATOR, PREFS_TYPE, 0);
  146.                 if (isHuman!=noErr)
  147.                     return prefs_cantCreatePrefsErr;
  148.                 newPrefs=TRUE;
  149.             }
  150.             else return prefs_cantOpenPrefsErr;
  151.         }
  152.         isHuman=FSpOpenDF(&prefsFile, fsRdWrPerm, &thisFile);
  153.         *prefsFileID=thisFile;
  154.         if (isHuman!=noErr)
  155.             return prefs_cantOpenPrefsErr;
  156.     }
  157.     else
  158.     {
  159.         isHuman=HOpen(vRefNum, dirID, name, fsRdWrPerm, &thisFile);
  160.         *prefsFileID=thisFile;
  161.         if (isHuman!=noErr)
  162.         {
  163.             if (isHuman==fnfErr)
  164.             {
  165.                 isHuman=HCreate(vRefNum, dirID, name, CREATOR, PREFS_TYPE);
  166.                 if (isHuman!=noErr)
  167.                     return prefs_cantCreatePrefsErr;
  168.                 prefsInfo.fdType=PREFS_TYPE;
  169.                 prefsInfo.fdCreator=CREATOR;
  170.                 prefsInfo.fdFlags=0;
  171.                 prefsInfo.fdLocation.h=prefsInfo.fdLocation.v=0;
  172.                 prefsInfo.fdFldr=0;
  173.                 isHuman=HSetFInfo(vRefNum, dirID, name, &prefsInfo);
  174.                 if (isHuman!=noErr)
  175.                     return prefs_cantCreatePrefsErr;
  176.                 isHuman=HOpen(vRefNum, dirID, name, fsRdWrPerm, &thisFile);
  177.                 *prefsFileID=thisFile;
  178.                 if (isHuman!=noErr)
  179.                     return prefs_cantOpenPrefsErr;
  180.                 newPrefs=TRUE;
  181.             }
  182.             else return prefs_cantOpenPrefsErr;
  183.         }
  184.     }
  185.     if (newPrefs)
  186.         return SetupNewPrefsFile(*prefsFileID);
  187.     
  188.     return prefs_allsWell;
  189. }
  190.  
  191. int SetupNewPrefsFile(int prefsFileID)
  192. {
  193.     int                err;
  194.     OSErr            isHuman;
  195.     long            count;
  196.     int                temp;
  197.     
  198.     gPrefsFilePos=2L;
  199.     isHuman=SetEOF(prefsFileID, 2L);
  200.     if (isHuman!=noErr)
  201.         return prefs_diskWriteErr;
  202.     SetFPos(prefsFileID, 1, 0L);
  203.     temp=PREFS_HEADER_VERSION;
  204.     count=2L;
  205.     isHuman=FSWrite(prefsFileID, &count, &temp);
  206.     if (isHuman!=noErr)
  207.         return prefs_diskWriteErr;        
  208.     err=Virgin(prefsFileID);
  209.     return err;
  210. }
  211.  
  212. void ClosePrefsFile(int prefsFileID)
  213. {
  214.     FSClose(prefsFileID);
  215.     FlushVol(0L, kOnSystemDisk);
  216. }
  217.  
  218. int GetNextPrefs(int prefsFileID)
  219. {
  220.     OSErr        isHuman;
  221.     long        count;
  222.     
  223.     count=sizeof(thePrefs);
  224.     isHuman=FSRead(prefsFileID, &count, &thePrefs);
  225.     if (isHuman==eofErr)
  226.         return prefs_noMorePrefsErr;
  227.     if (isHuman!=noErr)
  228.         return prefs_diskReadErr;
  229.     
  230.     return prefs_allsWell;
  231. }
  232.  
  233. int SavePrefs(int prefsFileID)
  234. {
  235.     long        oldEOF;
  236.     OSErr        isHuman;
  237.     long        count;
  238.     
  239.     GetEOF(prefsFileID, &oldEOF);
  240.     if (gPrefsFilePos>=oldEOF)
  241.     {
  242.         isHuman=SetEOF(prefsFileID, oldEOF+sizeof(thePrefs));
  243.         if (isHuman!=noErr)
  244.             return prefs_diskWriteErr;
  245.     }
  246.     SetFPos(prefsFileID, 1, gPrefsFilePos);
  247.     count=sizeof(thePrefs);
  248.     isHuman=FSWrite(prefsFileID, &count, &thePrefs);
  249.     if (isHuman!=noErr)
  250.         return prefs_diskWriteErr;
  251.     
  252.     return prefs_allsWell;
  253. }
  254.  
  255. int CheckVersion(int prefsFileID)
  256. {
  257.     OSErr        isHuman;
  258.     long        count;
  259.     int            temp;
  260.     
  261.     count=2L;
  262.     isHuman=FSRead(prefsFileID, &count, &temp);
  263.     if (isHuman!=noErr)
  264.         return prefs_diskReadErr;
  265.     if (temp>PREFS_HEADER_VERSION)
  266.         return prefs_versionNotSupportedErr;
  267.     if (temp<PREFS_HEADER_VERSION)
  268.         return SetupNewPrefsFile(prefsFileID);
  269.     
  270.     return prefs_allsWell;
  271. }
  272.  
  273. int GetFileID(void)
  274. {
  275.     CInfoPBRec        pb;
  276.     int                err;
  277.     
  278.     pb.hFileInfo.ioCompletion=0L;
  279.     pb.hFileInfo.ioNamePtr=CurApName;
  280.     pb.hFileInfo.ioVRefNum=0;
  281.     pb.hFileInfo.ioFDirIndex=0;
  282.     pb.hFileInfo.ioDirID=0;
  283.     err=PBGetCatInfo(&pb, FALSE);
  284.     if (err!=noErr)
  285.         return prefs_diskReadErr;
  286.     
  287.     gFileID=pb.hFileInfo.ioDirID;
  288.     
  289.     return prefs_allsWell;
  290. }
  291.  
  292. int CheckFileID(void)
  293. {
  294.     return (thePrefs.fileID==gFileID) ? prefs_allsWell : prefs_IDNotMatchErr;
  295. }
  296.  
  297. int Virgin(int prefsFileID)
  298. {
  299.     int            err;
  300.     
  301.     DefaultPrefs();
  302.     CopyGlobalsToPrefs();
  303.     err=SavePrefs(prefsFileID);
  304.     if (err!=prefs_allsWell)
  305.         return err;
  306.     DoSound(sound_virgin, TRUE);
  307.     GetRegistration();
  308.     CopyGlobalsToPrefs();
  309.     err=SavePrefs(prefsFileID);
  310.     
  311.     return (err==prefs_allsWell) ? prefs_virginErr : err;
  312. }
  313.  
  314. void DefaultPrefs(void)
  315. {
  316.     StuffHex(gMyName, "\p03426F62");    /* Bob */
  317.     gMyOrg[0]=0x00;
  318.     gSoundToggle=0xFF;
  319.     gShowMessageBox=0xFF;
  320.     gGameSpeed=0x02;
  321.     gComputerIntelligence=0x02;
  322.     gUseFullDictionary=0x00;
  323.     gNumComputerPlayers=1;
  324. }
  325.  
  326. void CopyGlobalsToPrefs(void)
  327. {
  328.     Mymemset(&thePrefs, 0, sizeof(thePrefs));
  329.     if (gMyName[0]>0x27)
  330.         gMyName[0]=0x27;
  331.     if (gMyOrg[0]>0x27)
  332.         gMyOrg[0]=0x27;
  333.     Mymemcpy(thePrefs.regname, gMyName, gMyName[0]+1);
  334.     Mymemcpy(thePrefs.regorg, gMyOrg, gMyOrg[0]+1);
  335.     thePrefs.sound=gSoundToggle;
  336.     thePrefs.messagebox=gShowMessageBox;
  337.     thePrefs.speed=gGameSpeed;
  338.     thePrefs.intelligence=gComputerIntelligence;
  339.     thePrefs.fulldict=gUseFullDictionary;
  340.     thePrefs.unused=0x00;
  341.     thePrefs.numcomputerplayers=gNumComputerPlayers;
  342.     thePrefs.fileID=gFileID;
  343. }
  344.  
  345. void CopyPrefsToGlobals(void)
  346. {
  347.     Mymemcpy(gMyName, thePrefs.regname, thePrefs.regname[0]+1);
  348.     Mymemcpy(gMyOrg, thePrefs.regorg, thePrefs.regorg[0]+1);
  349.     gSoundToggle=thePrefs.sound;
  350.     gShowMessageBox=thePrefs.messagebox;
  351.     gGameSpeed=thePrefs.speed;
  352.     gComputerIntelligence=thePrefs.intelligence;
  353.     gUseFullDictionary=thePrefs.fulldict;
  354.     gNumComputerPlayers=thePrefs.numcomputerplayers;
  355. }
  356.  
  357. void GetRegistration(void)
  358. {
  359.     DialogPtr        theDlog;
  360.     int                itemSelected = 0;
  361.     int                newleft;
  362.     int                newtop;
  363.     int                itemType;
  364.     Handle            item;
  365.     Rect            box;
  366.     int                dlogID;
  367.     
  368.     if(GetWindowDepth() == 1)
  369.         dlogID = bwPersonalDialog;
  370.     else
  371.         dlogID = colorPersonalDialog;
  372.         
  373.     theDlog = GetNewDialog(dlogID, 0L, (WindowPtr)-1L);
  374.     newleft = gMainScreenBounds.left + (((gMainScreenBounds.right -
  375.                 gMainScreenBounds.left) - (theDlog->portRect.right -
  376.                 theDlog->portRect.left)) / 2);
  377.     newtop = gMainScreenBounds.top + (((gMainScreenBounds.bottom -
  378.                 gMainScreenBounds.top) - (theDlog->portRect.bottom -
  379.                 theDlog->portRect.top)) / 2);
  380.     if(newtop < 15)
  381.         newtop = 15;
  382.     GetDItem(theDlog, 1, &itemType, &item, &box);
  383.     InsetRect(&box, -4, -4);
  384.     SetDItem(theDlog, 8, userItem, OutlineDefaultButton, &box);
  385.  
  386.     MoveWindow(theDlog, newleft, newtop, TRUE);
  387.     ShowWindow(theDlog);
  388.     while(itemSelected != 1)
  389.     {
  390.         ModalDialog(0L, &itemSelected);
  391.     }
  392.     GetDItem(theDlog,4,&itemType,&item,&box);
  393.     GetIText(item,&gMyName);
  394.     
  395.     GetDItem(theDlog,5,&itemType,&item,&box);
  396.     GetIText(item,&gMyOrg);
  397.  
  398.     if (gMyName[0]=='\0')
  399.         DefaultPrefs();
  400.  
  401.     HideWindow(theDlog);
  402.     DisposeDialog(theDlog);
  403. }
  404.  
  405. void SaveThePrefs(void)
  406. {
  407.     int            prefsFileID;
  408.     
  409.     if (gCanSavePrefs)
  410.     {
  411.         OpenPrefsFile(&prefsFileID);
  412.         CopyGlobalsToPrefs();
  413.         SavePrefs(prefsFileID);
  414.         ClosePrefsFile(prefsFileID);
  415.     }
  416. }
  417.